import base64
import time

from common import settings


def btoa(s):
    return base64.b64encode(s.encode()).decode()


def fetch(page: int):
    import requests

    cookies = {
        'Hm_lvt_434c501fe98c1a8ec74b813751d4e3e3': '1714036857',
        'Hm_lvt_9bcbda9cbf86757998a2339a0437208e': '1714381004,1714437986,1714464818,1714638753',
        'Hm_lvt_c99546cf032aaa5a679230de9a95c7db': '1714456622,1714464818,1714638753,1714964192',
        'qpfccr': 'true',
        'no-alert3': 'true',
        'tk': '-4071633681064532935',
        'sessionid': 'ymy5c0mfwamtqm1srz1h13xovupnfujy',
        'Hm_lpvt_c99546cf032aaa5a679230de9a95c7db': '1714964220',
    }

    headers = {
        'accept': 'application/json, text/javascript, */*; q=0.01',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        # 'cookie': 'Hm_lvt_434c501fe98c1a8ec74b813751d4e3e3=1714036857; Hm_lvt_9bcbda9cbf86757998a2339a0437208e=1714381004,1714437986,1714464818,1714638753; Hm_lvt_c99546cf032aaa5a679230de9a95c7db=1714456622,1714464818,1714638753,1714964192; qpfccr=true; no-alert3=true; tk=-4071633681064532935; sessionid=ymy5c0mfwamtqm1srz1h13xovupnfujy; Hm_lpvt_c99546cf032aaa5a679230de9a95c7db=1714964220',
        'pragma': 'no-cache',
        'priority': 'u=0, i',
        'referer': 'https://match.yuanrenxue.cn/match/12',
        'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest',
    }

    #  btoa('yuanrenxue' + window.page)
    # btoa('yuanrenxue1')
    # m:eXVhbnJlbnh1ZTE="
    m = btoa(f'yuanrenxue{page}')
    params = {
        'page': page,
        'm': m
    }

    response = requests.get('https://match.yuanrenxue.cn/api/match/12', params=params, cookies=cookies, headers=headers,
                            proxies=settings.proxies)
    print(response.json())
    return [x['value'] for x in response.json()['data']]


if __name__ == '__main__':
    values = []
    for page in range(1, 6):
        time.sleep(1)
        values.extend(fetch(page))
    print(sum(values))
